package com.enjoyor.health.HGPT.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.enjoyor.health.HGPT.core.entity.UserInfoMiniapp;
import com.enjoyor.health.HGPT.core.mapper.UserInfoMiniappMapper;
import com.enjoyor.health.HGPT.core.service.IUserInfoMiniappService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 小程序用户表 服务实现类
 * </p>
 *
 * @author enjoyor
 * @since 2023-10-18
 */
@Service
public class UserInfoMiniappServiceImpl extends ServiceImpl<UserInfoMiniappMapper, UserInfoMiniapp> implements IUserInfoMiniappService {

    @Autowired
    private UserInfoMiniappMapper userInfoMiniappMapper;

    @Override
    public UserInfoMiniapp getUserInfoMiniappByOpenId(String openId) {
        QueryWrapper<UserInfoMiniapp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("open_id", openId);
        return baseMapper.selectOne(queryWrapper);
    }

    public void insertOrUpdateUserInfoMiniapp(UserInfoMiniapp userInfoMiniapp) {
        String dateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        Map<String, Object> map = BeanUtil.beanToMap(userInfoMiniapp);
        map.put("open_id", userInfoMiniapp.getOpenId());
        map.put("user_id", userInfoMiniapp.getUserId());
        map.put("register_time", dateTime);
        map.put("storage_time", dateTime);
        map.put("update_time", dateTime);
        userInfoMiniappMapper.insertOrUpdateUserInfoMiniapp(map);
    }

    @Override
    public List<UserInfoMiniapp> getReservationUserList(String date, String period, Integer packageType) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("period", period);
        map.put("date", date);
        map.put("package_type", packageType);
        return baseMapper.getReservationUserList(map);
    }

    @Override
    public int updatePhoneAndUserId(String phone, String userId, String openId) {
        LambdaUpdateWrapper<UserInfoMiniapp> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(UserInfoMiniapp::getOpenId, openId)
                .set(UserInfoMiniapp::getPhone, phone)
                .set(UserInfoMiniapp::getUserId, userId);
        return baseMapper.update(null, lambdaUpdateWrapper);
    }

    @Override
    public List<UserInfoMiniapp> getByUserId(String userId) {
        QueryWrapper<UserInfoMiniapp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return baseMapper.selectList(queryWrapper);
    }
}
